<?php
class CacheHtml extends CacheBase
{
    protected $path;

    public function CacheHtml($path, $hour = null)
    {
        if (!AppConfig::GetInstance()->CACHE_HTML_MODE)
            $this->cachable = false;
        else
        {
            $this->path = String::RemoveTrailingSlash($path);
            if (!$hour) $hour = SettingModel::Get('CACHE_HOUR_'.str_replace(FS, '_', $this->path));
            if ($hour > 0) parent::__construct($hour, $this->GetCacheFile($path));
        }
    }

    protected function GetCacheFile($path)
    {
        $basePath = AppConfig::GetInstance()->DIR_CACHE;
        $path = 'html'.DS.AppConfig::GetInstance()->GetLanguageId().DS.str_replace(FS, DS, $this->path);
        FileHandler::MakeDirRecursive($path, $basePath);
        ksort($_GET);
        return $basePath.DS.$path.DS.md5(http_build_query($_GET)).'.tmp';
    }

    public function Render()
    {
        return FileHandler::Read($this->cacheFile);
    }

    public function Save($content)
    {
        if ($this->cachable)
            return FileHandler::Write($this->cacheFile, $content);
    }
}
?>
